

let fs = require('fs')
let iconvLite = require('iconv-lite')
let lj = 'E:\\tuo\\web\\reactNatiall\\src\\assets\\mp3\\gc\\'

let list = require('./src/qqmusic/musicList1')

let cccd = JSON.parse(JSON.stringify(list))

list.arr.forEach((itemc=>{
    let fileStr = fs.readFileSync(`${lj}${itemc.lrc}`,{encoding:'binary'});
    itemc.lrc = iconvLite.decode(fileStr,'GBK');

     itemc.lrc = itemc.lrc.split('\r\n').map((item)=>{

        if (!item) {
            return null
        }
        let ss = item.split(']')
        let scas = ss[ss.length-1]
        if(item.replace('[','').includes('[')){
            return [item.match(/\[(.+?)\]/g),scas]
        }else {
            let a =  item.split(']')
            if(a[0].includes('ti')||a[0].includes('by')||a[0].includes('al')||a[0].includes('ar')||a[0].includes('offset')){
                let cod = ''
                switch (a[0].split(':')[0].replace('[','')) {
                    case 'ti' :
                        cod = '歌曲名'
                        break;
                    case 'by' :
                        cod = '创作者'
                        break;
                    case 'al' :
                        cod = '专辑名'
                        break;
                    case 'ar' :
                        cod = '歌手'

                        break;
                }
                return {
                    time:0,
                    ci:a[0].replace('[','').replace(a[0].split(':')[0].replace('[',''),cod)
                }
            }else {

                let c2 = a[0].replace('[','').split(':')
                return {
                    time:(Number(c2[0])*60)+ Number(c2[1]),
                    ci:a[1]
                }

            }

        }

    })
    itemc.lrc.forEach(item=>{
        if(Array.isArray(item)){
            item[0].forEach(arr1=>{
                let c2 = arr1.replace('[','')
                c2 = c2.replace(']','')
                c2 = c2.split(':')
                itemc.lrc.push(
                    {
                        time:(Number(c2[0])*60)+ Number(c2[1]),
                        ci:item[1]
                    }
                )
            })
        }
    })


  itemc.lrc =  itemc.lrc.filter(item=>(item && !Array.isArray(item)))


    //数组对象排序
    function sortByKey(array,key){
        return array.sort(function(a,b){
            var x =  b[key];
            var y = a[key];
            return((x>y)?-1:((x<y)?1:0));
        })
    }
    itemc.lrc = sortByKey(itemc.lrc,'time')

}))


let str = JSON.stringify(list.arr)
str = str.replace(/(\"Microsoft)/g,'')
str = str.replace(/(Microsoft\")/g,'')
fs.writeFile('./src/qqmusic/musicList.js','export default' + str,'utf8',function(err){
    //如果err=null，表示文件使用成功，否则，表示希尔文件失败
    if(err){
        console.log('写文件出错了，错误是：'+err);
    }

    else{
        console.log('生成歌词OK');
    }

})




//计算滚动
let arr = []

cccd.arr.forEach((item)=>{
    if(!arr.includes(item.time)){
        arr.push(item.time)
    }
})


let arr1s = arr.map((itme,index1)=>{
    let dd = 0
    cccd.arr.forEach((sda=>{
        if( sda.time==itme){
            dd++
        }
    }))

    return {
        zjm:itme,
        gdgd:dd*59,
        cur:index1==0?true:false
    }
})


let sadassa =JSON.parse(JSON.stringify(arr1s))
sadassa.forEach((dsadsa,index)=>{
    if(!index){
        dsadsa.gdgd = 0
    }else {
        let shu = 0
        for(let i= 0; i<index ;i++){
            shu =  arr1s[i].gdgd + shu
        }
        dsadsa.gdgd = shu +25
    }

})

console.log(sadassa)
let strss = JSON.stringify(sadassa)
fs.writeFile('./src/qqmusic/daohang.js','export default' + strss,'utf8',function(err){
    //如果err=null，表示文件使用成功，否则，表示希尔文件失败
    if(err){
        console.log('写文件出错了，错误是：'+err);
    }

    else{
        console.log('生成导航OK');
    }

})
